﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Palluk.PassMaster.ViewModel
{
    public class DelegateCommand<T> : ICommand
    {
        private readonly Action<T> executeAction;

        private readonly Func<T, bool> canExecuteAction;

        public DelegateCommand(Action<T> executeAction)
            : this(executeAction, null)
        {
        }

        public DelegateCommand(Action<T> executeAction, Func<T, bool> canExecuteAction)
        {
            this.executeAction = executeAction;
            this.canExecuteAction = canExecuteAction;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            if (canExecuteAction != null)
            {
                return (canExecuteAction((T)parameter));
            }

            return executeAction != null;
        }

        public void Execute(object parameter)
        {
            if (executeAction != null)
            {
                executeAction((T)parameter);
            }
        }
    }
}
